﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PCControlCommand
{
    class MsgFilter : IMessageFilter
    {
        const int WM_PCCONTROL = 5317520;
        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_PCCONTROL)
            {
                MessageBox.Show("abc");
                return true;
            }
            return false;
        }
    }
    class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
            if (!runone)
            {
                return;
            }

            Application.AddMessageFilter(new MsgFilter());
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            return;
            PipServer pip = new PipServer("PCControl_Pipe");
            pip.ReceiveEvent += s =>
            {
                args = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                string cmd = args[0];
                Debug.WriteLine($"命令：{cmd}");
                switch (cmd)
                {
                    case "SendKey":
                        if (args.Length == 6)
                        {
                            Keys key = Keys.None; bool ctrl = false; bool shift = false; bool alt = false; bool win = false;
                            key = (Keys)uint.Parse(args[1]);
                            ctrl = bool.Parse(args[2]);
                            shift = bool.Parse(args[3]);
                            alt = bool.Parse(args[4]);
                            win = bool.Parse(args[5]);
                            PCControlService.TisnControl.SendKey(key, ctrl, shift, alt, win);
                        }
                        break;
                }
                Thread.Sleep(100);
            };
            pip.Listen();
            return;
            using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("PCControl_Pipe"))
            {
                pipeServer.BeginWaitForConnection((o) =>
                {
                    NamedPipeServerStream pServer = (NamedPipeServerStream)o.AsyncState;
                    pServer.EndWaitForConnection(o);
                    StreamReader sr = new StreamReader(pServer);
                    string tmp = "";
                    while (tmp != "Stop")
                    {
                        args = tmp.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                        string cmd = args[0];
                        Debug.WriteLine($"命令：{cmd}");
                        switch (cmd)
                        {
                            case "SendKey":
                                if (args.Length == 6)
                                {
                                    Keys key = Keys.None; bool ctrl = false; bool shift = false; bool alt = false; bool win = false;
                                    key = (Keys)uint.Parse(args[1]);
                                    ctrl = bool.Parse(args[2]);
                                    shift = bool.Parse(args[3]);
                                    alt = bool.Parse(args[4]);
                                    win = bool.Parse(args[5]);
                                    PCControlService.TisnControl.SendKey(key, ctrl, shift, alt, win);
                                }
                                break;
                        }
                        Thread.Sleep(100);
                    }
                }, pipeServer);
            }
        }
    }
}
